package com.biz.primus.ms.order.convertor;

import com.biz.primus.base.exception.BizSilentException;
import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.base.vo.PriceVo;
import com.biz.primus.base.vo.ProductSpecialOfferPromotionRespVo;
import com.biz.primus.base.vo.commodity.ProductBaseVo;
import com.biz.primus.base.vo.commodity.ProductStockVo;
import com.biz.primus.base.vo.commodity.SaleStatus;
import com.biz.primus.model.cart.enums.ShippingLevel;
import com.biz.primus.model.order.exception.OrderException;
import com.biz.primus.model.order.vo.OrderItemVO;
import com.biz.primus.model.order.vo.request.OrderItemRequestVO;
import com.biz.primus.model.order.vo.response.OrderItemResponseVO;
import com.biz.primus.ms.order.dao.po.Order;
import com.biz.primus.ms.order.dao.po.OrderItem;
import com.biz.primus.product.vo.OrderProductInfoRespVo;
import com.google.common.collect.Lists;
import org.codelogger.utils.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单明细转换器
 *
 * @author Tiger Feng
 * @date 2017年11月03日
 */
public class OrderItemConvertor {

    public static OrderItem vo2PO(OrderItemVO vo) {
        if (vo == null) {
            return null;
        }
        OrderItem po = new OrderItem();
        BeanUtils.copyProperties(vo, po);
        return po;
    }

    public static OrderItemVO po2VO(OrderItem po) {
        if (po == null) {
            return null;
        }
        OrderItemVO vo = new OrderItemVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

    public static List<OrderItem> vos2POs(List<OrderItemVO> vos) {
        if (vos == null) {
            return null;
        }
        if (vos.isEmpty()) {
            return Lists.newArrayList();
        }
        return vos.stream().map(vo -> vo2PO(vo)).collect(Collectors.toList());
    }

    public static List<OrderItemVO> pos2VOs(List<OrderItem> pos) {
        if (pos == null) {
            return null;
        }
        if (pos.isEmpty()) {
            return Lists.newArrayList();
        }
        return pos.stream().map(po -> po2VO(po)).collect(Collectors.toList());
    }

    public static OrderItemResponseVO po2ResponseVO(OrderItem po){
        if (po == null) {
            return null;
        }
        OrderItemResponseVO vo = new OrderItemResponseVO();
        BeanUtils.copyProperties(po,vo);
        return vo;
    }

    public static List<OrderItemResponseVO> pos2ResponseVOs(List<OrderItem> pos){
        if (pos == null) {
            return null;
        }
        if (pos.isEmpty()) {
            return Lists.newArrayList();
        }
        return pos.stream().map(po -> po2ResponseVO(po)).collect(Collectors.toList());
    }

    /**
     * OrderProductInfoRespVo 转 OrderItemVO
     *
     * @param itemRequestVO
     * @param isStoreOrder
     * @param isAppOrder
     * @param isWebOrder
     * @param productInfoRespVo
     */
    public static void productInfoRespVO2OrderItemRequestVO(OrderItemRequestVO itemRequestVO,
                                                            boolean isStoreOrder,
                                                            boolean isAppOrder,
                                                            boolean isWebOrder,
                                                            OrderProductInfoRespVo productInfoRespVo) {
        // 商品基本信息
        ProductBaseVo productBaseVo = productInfoRespVo.getProductBaseVo();
        AssertUtils.notNull(productBaseVo, OrderException.PRODUCT_INFO_NOT_FOUND);
        // 门店价格
        PriceVo depotPrice = productInfoRespVo.getProductPriceVo().getDepotPrice();
        // 总店价格
        PriceVo virtualPrice = productInfoRespVo.getProductPriceVo().getVirtualPrice();
        // 特价信息
        ProductSpecialOfferPromotionRespVo specialVO = productInfoRespVo.getSpecialOfferPromotion();
        // 库存信息
        ProductStockVo productStockVo = productInfoRespVo.getProductStockVo();
        AssertUtils.notNull(productStockVo, OrderException.PRODUCT_INFO_NOT_FOUND);
        if (isStoreOrder) {
            AssertUtils.notNull(depotPrice, OrderException.PRODUCT_INFO_NOT_FOUND);
            AssertUtils.notNull(depotPrice.getFinalPrice(), OrderException.PRODUCT_INFO_NOT_FOUND);
            // 判断库存数量是否满足
            if (itemRequestVO.getQuantity() > productStockVo.getStockRespVo().getWarehouseQuantity()) {
                throw new BizSilentException(OrderException.PRODUCT_STOCK_NOT_ENOUGH);
            }
        } else {
            AssertUtils.notNull(virtualPrice, OrderException.PRODUCT_INFO_NOT_FOUND);
        }
        if (isAppOrder && SaleStatus.OFF_SALE.getValue() == productBaseVo.getAppSaleStatus()) {
            // APP订单，但是商品未上架
            throw new BizSilentException(OrderException.PRODUCT_OFF_SALE_APP);
        }
        if (isWebOrder && SaleStatus.OFF_SALE.getValue() == productBaseVo.getWebSaleStatus()) {
            // 微商城订单，但是商品未上架
            throw new BizSilentException(OrderException.PRODUCT_OFF_SALE_WACHAT);
        }

        // 商品基本信息
        itemRequestVO.setProductName(productBaseVo.getName());
        itemRequestVO.setLogo(productBaseVo.getLogo());
        itemRequestVO.setProductId(productBaseVo.getProductId());
        itemRequestVO.setShippingLevel(packingShippingLevel(productInfoRespVo.getPredictTime()));
        itemRequestVO.setCategoryId(productBaseVo.getCategoryId());
        itemRequestVO.setWeight(productBaseVo.getWeight());
        itemRequestVO.setBrandId(productBaseVo.getBrandId());
        // 整箱数量
        itemRequestVO.setPackageNumber(productBaseVo.getPackageNumber());
        // 特价信息
        if (specialVO != null) {
            // 特价
            itemRequestVO.setSpecialOfferPrice(specialVO.getSpecialOfferPrice());
            // 特价能购买数量
            itemRequestVO.setSpecialOfferQuantityLimit(specialVO.getPurchaseQuantityLimit());
        }
        //价格信息
        if (isStoreOrder) {
            // 门店订单
            packingPrice(itemRequestVO, depotPrice);
        } else {
            // 总店订单
            packingPrice(itemRequestVO, virtualPrice);
        }
    }

    /**
     * 填装时间
     *
     * @param time
     * @return
     */
    private static ShippingLevel packingShippingLevel(Integer time) {
        switch (time) {
            case 19:
                return ShippingLevel.SHIPPING_RIGHT_NOW;
            case 29:
                return ShippingLevel.SHIPPING_24_HOURS;
            default:
                return ShippingLevel.SHIPPING_24_HOURS;
        }
    }

    /**
     * 填装价格
     *
     * @param itemRequestVO
     * @param priceVo
     */
    private static void packingPrice(OrderItemRequestVO itemRequestVO, PriceVo priceVo) {
        // 门店订单
        itemRequestVO.setMarketPrice(priceVo.getNormalPrice());
        itemRequestVO.setEWalletPrice(priceVo.getEwalletPrice());
        itemRequestVO.setFclSinglePrice(priceVo.getFclSinglePrice());
        if (priceVo.getFinalPrice() == null || priceVo.getFinalPrice() <= 0) {
            throw new BizSilentException(OrderException.PRODUCT_INFO_NOT_FOUND);
        }
        itemRequestVO.setMemberPrice(priceVo.getFinalPrice());
        itemRequestVO.setFinalPrice(null);
    }

    public static OrderItem responseVO2PO(OrderItemResponseVO vo, Order order) {
        if (vo == null) {
            return null;
        }
        OrderItem po = new OrderItem();
        BeanUtils.copyProperties(vo, po);
        po.setOrder(order);
        return po;
    }
}